package cn.yitun.chat.yitunserverimapicore.business.group.service.impl;

import cn.yitun.chat.yituncommonmessage.ExceptionEnum;
import cn.yitun.chat.yituncommonmessage.result.ResultMessage;
import cn.yitun.chat.yituncommonutil.YitunCollectionUtil;
import cn.yitun.chat.yitunservercommonlib.crud.yitun.CrudServiceImpl;
import cn.yitun.chat.yitunservercommonlib.dataentity.dto.group.GroupRelationDto;
import cn.yitun.chat.yitunservercommonlib.dataentity.po.group.GroupRelationInfo;
import cn.yitun.chat.yitunservercommonlib.dataentity.vo.group.GroupRelationVo;
import cn.yitun.chat.yitunservercommonlib.util.NumUtil;
import cn.yitun.chat.yitunservercommonlib.util.R;
import cn.yitun.chat.yitunservercommonlib.util.codec.IdGen;
import cn.yitun.chat.yitunserverimapicore.business.group.service.GroupRelationService;
import cn.yitun.chat.yitunserverimbase.dao.group.GroupRelationDao;
import cn.yitun.chat.yitunserverimcore.general.main.push.group.GroupRelationPush;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * Date 2019-01-20 21:26:21<br>
 * Description
 * @author yitun
 * @since 1.0.0
 */
@Service
@Transactional
public class GroupRelationServiceImpl
		extends CrudServiceImpl<GroupRelationDao, GroupRelationDto, GroupRelationInfo, GroupRelationVo>
		implements GroupRelationService {

	@Resource
	private GroupRelationPush groupRelationPush;

	/**
	 * Date 2019-01-20 17:31:47<br>
	 * Description 获取列表
	 * @author yitun
	 * @param userId
	 * @return
	 * @since 1.0.0
	 */
	public List<GroupRelationVo> getListByUserId(Long userId) {
		List<GroupRelationVo> list = dao.listVO(
				R.param("userId", userId));
		return list;
	}

	/**
	 * Date 2019-01-27 09:50:38<br>
	 * Description
	 * @author yitun
	 * @param userId
	 * @param groupId
	 * @return
	 * @since 1.0.0
	 */
	public GroupRelationVo getByGroupId(Long userId, Long groupId) {
		return dao.getVOByGroupId(userId, groupId);
	}

	/**
	 * Date 2019-01-20 21:18:07<br>
	 * Description 移动群到其它分组
	 * @author yitun
	 * @param userId
	 * @param groupIds
	 * @param categoryId
	 * @return
	 * @since 1.0.0
	 */
	public ResultMessage moveCategory(Long key, Long userId, List<Long> groupIds, Long categoryId) {
		ResultMessage message = new ResultMessage();
		try {

			if (YitunCollectionUtil.isNotEmpty(groupIds)) {
				int count = 0;
				for (Long groupId : groupIds) {
					count = count + dao.updateGroupCategoryId(userId, groupId, categoryId);
				}
				if (count <= 0) {
					message.addWarning(ExceptionEnum.DO_FAIL);
				} else {
					groupRelationPush.pushMoveCategory(key, groupIds, categoryId, userId);
				}
			}

		} catch (Exception e) {
			message.addWarning(ExceptionEnum.BUSY_SERVER);
		}
		return message;
	}

	/**
	 * Date 2019-01-20 14:17:03<br>
	 * Description 修改备注
	 * @author yitun
	 * @param userId
	 * @param groupId
	 * @param remark
	 * @return
	 * @since 1.0.0
	 */
	public ResultMessage updateRemark(Long key, Long userId, Long groupId, String remark) {

		ResultMessage message = new ResultMessage();
		try {

			if (NumUtil.isValid(groupId)) {
				int count = dao.updateRemark(userId, groupId, remark);
				if (count <= 0) {
					message.addWarning(ExceptionEnum.DO_FAIL);
				} else {
					groupRelationPush.pushUpdateRemark(IdGen.getNumber(), groupId, remark, userId);
				}
			} else {
				message.addWarning(ExceptionEnum.BUSY_SERVER);
			}
		} catch (Exception e) {
			message.addWarning(ExceptionEnum.BUSY_SERVER);
		}
		return message;
	}
}
